﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace alicebob
{

   
    public partial class Form1 : Form
    {

       

        int[] primes = { 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,47,49,53,59,61,67,71,73,79,83,89,97};
        int flag = 0;
        int cpuselected;
        String lastnum;

        public bool prim(int num)
        {
            foreach(int i in primes)
            {
                if (num == i)
                    return true;
            }
            return false;
        }
        public Form1()
        {
            InitializeComponent();

            Random r = new Random(Environment.TickCount);
            int x;
            while (true)
            {
                x = r.Next() % 100;
                if (x>40)
                    break;
            }

            currentnumber.Text = x.ToString();
            lastnum = x.ToString();
        }

        private void play_Click(object sender, EventArgs e)
        {
            int getnum=0;
            Int32.TryParse(inputnumber.Text, out getnum);

            if (getnum == 0)
            {
                MessageBox.Show("Enter a valid number");
            }
            else
               

                
            
            if ((Int32.Parse(currentnumber.Text) - getnum) < 1)
            {
                MessageBox.Show("Pick a prime number less than current number");

            }
            else
                if (prim(getnum) == false)
                {
                    MessageBox.Show("Enter only prime number ");
                }

                else
                {

                    currentnumber.Text = (Int32.Parse(currentnumber.Text) - getnum).ToString();

                    if (currentnumber.Text == "1")
                    {
                        MessageBox.Show("You played " + lastnum.ToString() + " - " + getnum + " ,You reached the number 1.\n\nYOU WIN");
                        lastnum = (Int32.Parse(lastnum) - getnum).ToString();
                    }
                    else
                    {
                        MessageBox.Show("You played " + lastnum.ToString() + " - " + getnum + ", current number is " + currentnumber.Text);
                        lastnum = (Int32.Parse(lastnum) - getnum).ToString();
                        flag = 0;
                        for (int i = primes.Length - 1; i >= 0; i--)
                        {
                            if ((Int32.Parse(currentnumber.Text) - primes[i]) % 4 == 1)
                            {
                                cpuselected = primes[i];
                                flag = 1;
                                break;
                            }

                        }

                        if (flag == 1)
                        {
                            currentnumber.Text = (Int32.Parse(currentnumber.Text) - cpuselected).ToString();
                            MessageBox.Show("computer played " + lastnum.ToString() + " - " + cpuselected + ", current number is " + currentnumber.Text);
                            lastnum = (Int32.Parse(lastnum) - cpuselected).ToString();
                        }
                        else
                        {
                            currentnumber.Text = (Int32.Parse(currentnumber.Text) - 1).ToString();
                            MessageBox.Show("computer played 1 ,current number is " + currentnumber.Text);
                            lastnum = (Int32.Parse(lastnum) - cpuselected).ToString();
                        }

                        if (currentnumber.Text == "1")
                        {
                            MessageBox.Show("CPU reached the number 1.\n\nCPU WINS");
                        }

                    }


                }

        }

        private void inputnumber_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Restart();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show(" The rules are simple:\nA random number between 40 and 100 will be selected.\nYou and CPU play the game in turns. In each turn, you have to subtract a prime number less than the current number.\nThe one who reaches the number 1 first, wins.");
        }
    }
}
